package org.dromara.basic.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.basic.domain.bo.AcademicYearBo;
import org.dromara.basic.domain.vo.AcademicYearVo;
import org.dromara.basic.service.IAcademicYearService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学年管理
 *
 * @author Lion Li
 * @date 2024-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/basic/academicYear")
public class AcademicYearController extends BaseController {

    private final IAcademicYearService academicYearService;

    /**
     * 查询学年管理列表
     */
//    @SaCheckPermission("basic:academicYear:list")
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<AcademicYearVo> list(AcademicYearBo bo, PageQuery pageQuery) {
        return academicYearService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询学年管理列表
     */
//    @SaCheckPermission("basic:academicYear:list")
    @SaIgnore
    @GetMapping("/infoList")
    public R<List<AcademicYearVo>> infoList(AcademicYearBo bo) {
        return R.ok(academicYearService.queryList(bo));
    }

    /**
     * 导出学年管理列表
     */
    @SaCheckPermission("basic:academicYear:export")
    @Log(title = "学年管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AcademicYearBo bo, HttpServletResponse response) {
        List<AcademicYearVo> list = academicYearService.queryList(bo);
        ExcelUtil.exportExcel(list, "学年管理", AcademicYearVo.class, response);
    }

    /**
     * 获取学年管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("basic:academicYear:query")
    @GetMapping("/{id}")
    public R<AcademicYearVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(academicYearService.queryById(id));
    }

    /**
     * 新增学年管理
     */
    @SaCheckPermission("basic:academicYear:add")
    @Log(title = "学年管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AcademicYearBo bo) {
        return toAjax(academicYearService.insertByBo(bo));
    }

    /**
     * 修改学年管理
     */
    @SaCheckPermission("basic:academicYear:edit")
    @Log(title = "学年管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AcademicYearBo bo) {
        return toAjax(academicYearService.updateByBo(bo));
    }
    /**
     * 修改学年管理
     */
    @SaCheckPermission("basic:academicYear:edit")
    @Log(title = "学年管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/switch")
    public R<Void> switchAcademic(@Validated(EditGroup.class) @RequestBody AcademicYearBo bo) {
        return toAjax(academicYearService.switchAcademic(bo));
    }

    /**
     * 删除学年管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("basic:academicYear:remove")
    @Log(title = "学年管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(academicYearService.deleteWithValidByIds(List.of(ids), true));
    }
}
